import java.io.*;
public class CellularAutomaton {
	public static void main(String[] args) throws Exception{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int R=Integer.parseInt(br.readLine());
		String[] grid=new String[R];
		for(int i=0;i<R;i++){
			grid[i]=br.readLine();
		}
		String rules=br.readLine();
		int N=Integer.parseInt(br.readLine());
		int K=Integer.parseInt(br.readLine());
		String[]res=configure(grid,rules,N,K);
		for(int i=0;i<R;i++)System.out.println(res[i]);
	}
	public static String[] configure(String[] grid, String rules, int N, int K) {
		String[] curgrid = grid.clone();
		for (int i = 0; i < K; i++)
			curgrid = process(curgrid, rules);
		int livingoriginal = howmanyliving(curgrid); // No changes
		int onebest = livingoriginal;
		// This version only changes one character
		curgrid = grid.clone();
		String[] onechangebest = curgrid.clone();
		for (int i = 0; i < grid.length; i++) {
			for (int j = 0; j < grid[i].length(); j++) {
				char[] cur = grid[i].toCharArray();
				curgrid=grid.clone();
				if (cur[j] == '0')
					cur[j] = '1';
				else
					cur[j] = '0';
				curgrid[i] = "";
				for (int k = 0; k < grid[0].length(); k++)
					curgrid[i] = curgrid[i] + cur[k];
				String[] resgrid = process(curgrid, rules);
				for (int k = 0; k < K - 1; k++)
					resgrid = process(resgrid, rules);
				int curliv = howmanyliving(resgrid);
				if (curliv > onebest) {
					onebest = curliv;
					onechangebest = curgrid.clone();
				}
				if(j>50)break;
			}
			if(i>50)break;
		}
		return onechangebest;
	}

	public static String[] process(String[] origgrid, String rules) {
		String[] ans;
		char[][] grid = new char[origgrid.length][100];
		for (int i = 0; i < origgrid.length - 1; i++) {
			grid[i] = origgrid[i].toCharArray();
		}
		int x = origgrid.length - 1, y = grid[0].length - 1;
		int[][] L = new int[x + 1][y + 1];
		char[] rules1 = rules.toCharArray();
		// Find L
		for (int i = 0; i <= x; i++) {
			for (int j = 0; j <= y; j++) {
				int living = 0;
				// Regular points
				if (i != 0 && j != 0 && i != x && j != y) {
					if (grid[i][j - 1] == '1')
						living++;
					if (grid[i][j + 1] == '1')
						living++;
					if (grid[i - 1][j] == '1')
						living++;
					if (grid[i + 1][j] == '1')
						living++;
					if (grid[i - 1][j - 1] == '1')
						living++;
					if (grid[i - 1][j + 1] == '1')
						living++;
					if (grid[i + 1][j - 1] == '1')
						living++;
					if (grid[i + 1][j + 1] == '1')
						living++;
				}
				// Four corners
				else if (i == 0 && j == 0) {
					if (grid[x][y] == '1')
						living++;
					if (grid[x][0] == '1')
						living++;
					if (grid[x][1] == '1')
						living++;
					if (grid[0][y] == '1')
						living++;
					if (grid[1][y] == '1')
						living++;
					if (grid[1][0] == '1')
						living++;
					if (grid[0][1] == '1')
						living++;
					if (grid[1][1] == '1')
						living++;
				} else if (i == x && j == y) {
					if (grid[0][0] == '1')
						living++;
					if (grid[x][y - 1] == '1')
						living++;
					if (grid[x - 1][y] == '1')
						living++;
					if (grid[x - 1][y - 1] == '1')
						living++;
					if (grid[0][y] == '1')
						living++;
					if (grid[0][y - 1] == '1')
						living++;
					if (grid[x][0] == '1')
						living++;
					if (grid[x - 1][0] == '1')
						living++;
				} else if (i == 0 && j == y) {
					if (grid[0][0] == '1')
						living++;
					if (grid[1][0] == '1')
						living++;
					if (grid[x][0] == '1')
						living++;
					if (grid[x][y] == '1')
						living++;
					if (grid[x][y - 1] == '1')
						living++;
					if (grid[0][y - 1] == '1')
						living++;
					if (grid[1][y] == '1')
						living++;
					if (grid[1][y - 1] == '1')
						living++;
				} else if (i == x && j == 0) {
					if (grid[0][0] == '1')
						living++;
					if (grid[0][1] == '1')
						living++;
					if (grid[x][y] == '1')
						living++;
					if (grid[x - 1][y] == '1')
						living++;
					if (grid[0][y] == '1')
						living++;
					if (grid[x - 1][0] == '1')
						living++;
					if (grid[x][1] == '1')
						living++;
					if (grid[x - 1][1] == '1')
						living++;
				}
				// Upper
				else if (i == 0 && j > 0 && j < y) {
					if (grid[0][j - 1] == '1')
						living++;
					if (grid[0][j + 1] == '1')
						living++;
					if (grid[x][j] == '1')
						living++;
					if (grid[1][j] == '1')
						living++;
					if (grid[x][j - 1] == '1')
						living++;
					if (grid[x][j + 1] == '1')
						living++;
					if (grid[1][j - 1] == '1')
						living++;
					if (grid[1][j + 1] == '1')
						living++;
				}
				// Down
				else if (i == x && j > 0 && j < y) {
					if (grid[x][j - 1] == '1')
						living++;
					if (grid[x][j + 1] == '1')
						living++;
					if (grid[x - 1][j] == '1')
						living++;
					if (grid[0][j] == '1')
						living++;
					if (grid[x - 1][j - 1] == '1')
						living++;
					if (grid[x - 1][j + 1] == '1')
						living++;
					if (grid[0][j - 1] == '1')
						living++;
					if (grid[0][j + 1] == '1')
						living++;
				}
				// Left
				else if (j == 0 && i > 0 && i < x) {
					if (grid[i][y] == '1')
						living++;
					if (grid[i][1] == '1')
						living++;
					if (grid[i - 1][0] == '1')
						living++;
					if (grid[i + 1][0] == '1')
						living++;
					if (grid[i - 1][y] == '1')
						living++;
					if (grid[i - 1][1] == '1')
						living++;
					if (grid[i + 1][y] == '1')
						living++;
					if (grid[i + 1][1] == '1')
						living++;
				}
				// Right
				else if (j == y && i > 0 && i < x) {
					if (grid[i][y - 1] == '1')
						living++;
					if (grid[i][0] == '1')
						living++;
					if (grid[i - 1][y] == '1')
						living++;
					if (grid[i + 1][y] == '1')
						living++;
					if (grid[i - 1][y - 1] == '1')
						living++;
					if (grid[i - 1][0] == '1')
						living++;
					if (grid[i + 1][y - 1] == '1')
						living++;
					if (grid[i + 1][0] == '1')
						living++;
				}
				L[i][j] = living;
			}
		}
		for (int i = 0; i <= x; i++) {
			for (int j = 0; j <= y; j++) {
				char currule = rules1[L[i][j]];
				if (currule == '+')
					grid[i][j] = '1';
				if (currule == '-')
					grid[i][j] = '0';
				if (currule == 'X' && grid[i][j] == '1')
					grid[i][j] = '0';
				if (currule == 'X' && grid[i][j] == '0')
					grid[i][j] = '1';
			}
		}
		ans = new String[origgrid.length];
		for (int i = 0; i < origgrid.length; i++) {
			ans[i] = "";
			for (int j = 0; j < origgrid[0].length(); j++)
				ans[i] = ans[i] + grid[i][j];
		}
		return ans;
	}

	public static int howmanyliving(String[] origgrid) {
		int living = 0;
		for (int i = 0; i < origgrid.length; i++) {
			char[] cur = origgrid[i].toCharArray();
			for (int j = 0; j < cur.length; j++)
				if (cur[j] == '1')
					living++;
		}
		return living;
	}
}
